home *** CD-ROM | disk | FTP | other *** search
/ Packard Bell - Internet on a CD / internet on a cd.cdr / Internet / sites / Clementine_NASA / clemdsrc.hqx / scroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-24  |  8.7 KB  |  428 lines

  1. /**************************************************************************************
  2. * scroll subroutine - Adapted from MPW toolkit C examples
  3. *
  4. * program description
  5. *
  6. *     Display a window with help information and add a vertical scroll bar to it
  7. *
  8. * argument descriptions
  9. *
  10. *     argument        use
  11. *    --------        ---
  12. *
  13. *    winres            The resource id of the window resource.
  14. *
  15. *    vscres            The resource id of the scroll bar control.
  16. *
  17. ***************************************************************************************/
  18.  
  19. /********************
  20. * Standard C includes
  21. *********************/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. /***************************
  27. * Macintosh toolbox includes
  28. ****************************/
  29. #include <values.h>
  30. #include <traps.h>
  31. #include <Packages.h>
  32.  
  33. /***************** 
  34. * Global variables
  35. ******************/
  36. static TEHandle docTE;
  37. static ControlHandle docVScroll;
  38. static ProcPtr docClik;
  39.  
  40. /*pascal void PascalClikLoop();
  41. pascal ProcPtr GetOldClikLoop();*/
  42.  
  43. void scroll (char *hlpfil,char *prognam,long int *winres,long int *vscres)
  44. {
  45.   WindowPtr  window;
  46.   Rect  viewRect,destRect;
  47.   char  bufr[80],txtfil[8000];
  48.   char  *txt;
  49.   char  pname[9];
  50.   long int  txtlen;
  51.   extern long int  resnumq;
  52.   long int  hlpnum = 400;
  53.   int  nlines,i;
  54.   FILE  *hlp;
  55.   
  56.   void errmsg();
  57.   void GetTRect();
  58.   void AdjustVwRect();
  59.   void EvntLoop();
  60.   pascal void AsmClikLoop();
  61.   
  62.   txt = txtfil;
  63.   strcpy(txt,"");
  64.   
  65.   hlp = fopen(hlpfil,"rb");
  66.   if (hlp == NULL) {
  67.     errmsg("Error opening the help text file",&hlpnum);
  68.     return;
  69.   }
  70.   
  71.   fgets(bufr,80,hlp);
  72.   while (!feof(hlp)) {
  73.     strcat(txt,bufr);
  74.     fgets(bufr,80,hlp);
  75.   }
  76.   i = fclose(hlp);
  77.  
  78.   window = GetNewWindow(*winres,NULL,(WindowPtr) -1);
  79.   
  80.   if (window != NULL) {
  81.     SetPort(window);
  82.     TextFont(4);
  83.     TextSize(12);
  84.     strcpy(pname,prognam);
  85.     SetWTitle(window,c2pstr(pname));
  86.     GetTRect(window,&viewRect);
  87.     destRect = viewRect;
  88.     destRect.bottom = destRect.top + 100;
  89.     docTE = TENew(&destRect,&viewRect);
  90.     
  91.     if (docTE != NULL) {
  92.       AdjustVwRect(docTE);
  93.       TEAutoView(true,docTE);
  94.       docClik = (ProcPtr) (*docTE)->clikLoop;
  95.       (*docTE)->clikLoop = (ClikLoopProcPtr) AsmClikLoop;
  96.       docVScroll = GetNewControl(*vscres,window);
  97.   
  98.       if (docVScroll != NULL) {
  99.         txtlen = strlen(txt);
  100.     TESetText(txt,txtlen,docTE);
  101.     nlines = (*docTE)->nLines - 27;
  102.     if (nlines < 0) nlines = 0;
  103.     SetCtlMin(docVScroll,0);
  104.     SetCtlMax(docVScroll,nlines);
  105.     SetCtlValue(docVScroll,0);
  106.     TEUpdate(&viewRect,docTE);
  107.     ShowWindow(window);
  108.     ShowControl(docVScroll);
  109.     DrawControls(window);
  110.     HiliteControl(docVScroll,0);
  111.       }
  112.       else {
  113.     errmsg("Error reading control template from resource file",&resnumq);
  114.     DisposeWindow(window);
  115.     exit(0);
  116.       }
  117.     }
  118.     else {
  119.       errmsg("Error creating an edit record for the window",&resnumq);
  120.       DisposeWindow(window);
  121.       exit(0);
  122.     }
  123.   }
  124.   else {
  125.     errmsg("Error reading window template from the resource file",&resnumq);
  126.     DisposeWindow(window);
  127.     exit(0);
  128.   }
  129.   
  130.   EvntLoop();
  131. }
  132.  
  133. /******************************
  134. * GetTRect subroutine 
  135. *
  136. * subroutine description
  137. *
  138. * argument descriptions
  139. *
  140. *******************************/
  141.  
  142. void GetTRect (WindowPtr window,Rect *teRect)
  143. {
  144.   *teRect = window->portRect;
  145.   InsetRect(teRect,2,2);
  146.   teRect->right = teRect->right - 15;
  147. }
  148.  
  149. /*********************************
  150. * AdjustVwRect subroutine 
  151. *
  152. * subroutine description
  153. *
  154. * argument description
  155. *
  156. **********************************/
  157.  
  158. void AdjustVwRect (TEHandle docTE)
  159. {
  160.   TEPtr  te;
  161.   
  162.   te = *docTE;
  163.   te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top)/
  164.                         te->lineHeight) * te->lineHeight) +
  165.                         te->viewRect.top;
  166. }
  167.  
  168. /*********************************
  169. * AdjstHV subroutine 
  170. *
  171. * subroutine description
  172. *
  173. * argument description
  174. *
  175. **********************************/
  176.  
  177. void AdjstHV (Boolean Redraw)
  178. {
  179.   short  oldValue,oldMax,lines,max,value;
  180.   TEPtr  te;
  181.   
  182.   oldValue = GetCtlValue(docVScroll);
  183.   oldMax = GetCtlMax(docVScroll);
  184.   te = *docTE;
  185.   lines = te->nLines;
  186.   if (*(*te->hText + te->teLength - 1) == 13)
  187.     lines += 1;
  188.   max = lines - ((te->viewRect.bottom - te->viewRect.top)/
  189.                 te->lineHeight);
  190.  
  191.   if (max < 0) max = 0;
  192.   SetCtlMax(docVScroll,max);
  193.   
  194.   te = *docTE;
  195.   value = (te->viewRect.top - te->destRect.top)/te->lineHeight;
  196.   
  197.   if (value < 0) value = 0;
  198.   else if (value > max) value = max;
  199.   
  200.   SetCtlValue(docVScroll,value);
  201.   if (Redraw || (max != oldMax) || (value != oldValue))
  202.     ShowControl(docVScroll);
  203. }
  204.  
  205. /************************************
  206. * EvntLoop subroutine 
  207. *
  208. * subroutine description
  209. *
  210. *************************************/
  211.  
  212. void EvntLoop ()
  213. {
  214.   EventRecord  event;
  215.   Boolean  gotEvent;
  216.   short  part;
  217.   WindowPtr  window;
  218.   
  219.   void DoContentClick();
  220.   void DoCloseWindow();
  221.   
  222.   do {
  223.     SystemTask();
  224.     gotEvent = GetNextEvent(everyEvent,&event);
  225.     
  226.     if (gotEvent) {
  227.       switch (event.what) {
  228.         case mouseDown:
  229.       part = FindWindow(event.where,&window);
  230.       switch (part) {
  231.         case inContent:
  232.           DoContentClick(window,&event);
  233.           break;
  234.         case inGoAway:
  235.           if (TrackGoAway(window,event.where))
  236.             DoCloseWindow(window);
  237.           break;
  238.       }
  239.       break;
  240.       }
  241.     }
  242.   } while (part != inGoAway);
  243. }
  244.  
  245. /**************************************
  246. * DoContentClick subroutine 
  247. * subroutine description
  248. *
  249. * argument descriptions
  250. *
  251. ***************************************/
  252.  
  253. void DoContentClick (WindowPtr window,EventRecord *event)
  254. {
  255.   Point  mouse;
  256.   Rect  teRect;
  257.   ControlHandle  control;
  258.   short  part,value;
  259.   
  260.   void GetTRect();
  261.   pascal void VActnProc();
  262.   
  263.   SetPort(window);
  264.   mouse = event->where;
  265.   GlobalToLocal(&mouse);
  266.   GetTRect(window,&teRect);
  267.   if (!(PtInRect(mouse,&teRect))) {
  268.     part = FindControl(mouse,window,&control);
  269.     switch (part) {
  270.       case 0:
  271.         break;
  272.       case inThumb:
  273.         value = GetCtlValue(control);
  274.     part = TrackControl(control,mouse,NULL);
  275.     if (part != 0) {
  276.       value -= GetCtlValue(control);
  277.       if (value != 0)
  278.         TEScroll(0,value*(*docTE)->lineHeight,docTE);
  279.     }
  280.     break;
  281.       default:
  282.         value = TrackControl(control,mouse,(ProcPtr) VActnProc);
  283.     break;
  284.     }
  285.   }
  286. }
  287.  
  288. /**********************************
  289. * VActnProc subroutine 
  290. *
  291. * subroutine description
  292. *
  293. * argument descriptions
  294. *
  295. ***********************************/
  296.  
  297. pascal void VActnProc (ControlHandle control,short part)
  298. {
  299.   WindowPtr  window;
  300.   TEPtr  te;
  301.   short  amount;
  302.   
  303.   void CommonAction();
  304.   
  305.   if (part != 0) {
  306.     window = (*control)->contrlOwner;
  307.     te = *docTE;
  308.     switch (part) {
  309.       case inUpButton:
  310.       case inDownButton:
  311.         amount = 1;
  312.     break;
  313.       case inPageUp:
  314.       case inPageDown:
  315.         amount = (te->viewRect.bottom - te->viewRect.top)/
  316.              te->lineHeight;
  317.     break;
  318.     }
  319.     if ((part == inDownButton) || (part == inPageDown))
  320.       amount = -amount;
  321.     CommonAction(control,&amount);
  322.     if (amount != 0)
  323.       TEScroll(0,amount*te->lineHeight,docTE);
  324.   }
  325. }
  326.  
  327. /*******************************
  328. * CommonAction subroutine 
  329. *
  330. * subroutine description
  331. *
  332. * argument descriptions
  333. *
  334. ********************************/
  335.  
  336. void CommonAction (ControlHandle control,short *amount)
  337. {
  338.   short  value,max;
  339.   
  340.   value = GetCtlValue(control);
  341.   max = GetCtlMax(control);
  342.   *amount = value - *amount;
  343.   if (*amount < 0)
  344.     *amount = 0;
  345.   else if (*amount > max)
  346.     *amount = max;
  347.   SetCtlValue(control,*amount);
  348.   *amount = value - *amount;
  349. }
  350.  
  351. /**************************************
  352. * DoCloseWindow subroutine 
  353. *
  354. * subroutine description
  355. *
  356. * argument description
  357. *
  358. ***************************************/
  359.  
  360. void DoCloseWindow (WindowPtr window)
  361. {
  362.   TEHandle  te;
  363.   
  364.   te = docTE;
  365.   if (te != NULL)
  366.     TEDispose(te);
  367.   DisposeWindow(window);
  368. }
  369.  
  370. /*********************************
  371. * PascalClikLoop subroutine 
  372. *
  373. * subroutine description
  374. *
  375. **********************************/
  376.  
  377. pascal void PascalClikLoop ()
  378. {
  379.   WindowPtr  window;
  380.   RgnHandle  region;
  381.   
  382.   
  383.   window = FrontWindow();
  384.   region = NewRgn();
  385.   GetClip(region);
  386.   ClipRect(&window->portRect);
  387.   AdjstHV(true);
  388.   SetClip(region);
  389.   DisposeRgn(region);
  390. }
  391.  
  392. /**********************************
  393. * GetOldClikLoop subroutine 
  394. *
  395. * subroutine description
  396. *
  397. ***********************************/
  398.  
  399. pascal ProcPtr GetOldClikLoop ()
  400. {
  401.   return docClik;
  402. }
  403.  
  404.  
  405. /*pascal void AsmClikLoop()
  406. {
  407.  
  408. asm {
  409.             
  410.             MOVEM.L        D1-D2/A1,-(SP)        ; D0 and A0 need not be saved
  411.             CLR.L        -(SP)                ; make space for procedure pointer
  412.             JSR            GetOldClikLoop        ; get the old clikLoop
  413.             MOVEA.L        (SP)+,A0            ; into A0
  414.             MOVEM.L        (SP)+,D1-D2/A1        ; restore the world as it was
  415.             
  416.             JSR            (A0)                ; and execute old clikLoop
  417.  
  418.             MOVEM.L        D1-D2/A1,-(SP)        ; D0 and A0 need not be saved
  419.             JSR            PascalClikLoop        ; do our clikLoop
  420.             MOVEM.L        (SP)+,D1-D2/A1        ; restore the world as it was
  421.             MOVEQ        #1,D0                ; clear the zero flag so TextEdit keeps going
  422.             RTS
  423. }
  424. }
  425. */
  426.  
  427.